package cn.slipi.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping(value = "/http")
public class HttpController {


    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @GetMapping(value = "/consumer/health")
    public String getConsumerHealthStatus() {
        ServiceInstance si = this.loadBalancerClient.choose("consumer");
        String url = String.format("http://%s:%s/%s", si.getHost(), si.getPort(), "health");

        RestTemplate restTemplate = new RestTemplate();
        String res = restTemplate.getForObject(url, String.class);

        return "result : " + res;
    }
}
